﻿using UnityEngine;

namespace MyResKit
{
    public class AssetRes : Res
    {
        private string mAssetBundleName ;

        public AssetRes(string assetName, string assetBundleName) : base(assetName)
        {
            mAssetBundleName = assetBundleName ?? AssetTable.Instance.GetAssetBundleName(assetName);
        }

        private AssetBundleRes mAssetBundleRes;

        public override void Load()
        {
            if (mAssetBundleRes == null)
            {
                mAssetBundleRes =
                    ResMgr.GetBundleRes(Application.streamingAssetsPath + "/AssetBundles/" + mAssetBundleName);
            }
            
            mAssetBundleRes.Retain();
            mAsset = (mAssetBundleRes.Asset as AssetBundle).LoadAsset(Name);
        }

        protected override void Unload()
        {
            if (mAsset is GameObject)
            {
            }
            else
            {
                Resources.UnloadAsset(mAsset);
            }

            mAssetBundleRes.Release();
            mAssetBundleRes = null;
        }
    }
}